package com.bupt.ilink.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bupt.ilink.entity.*;
import com.bupt.ilink.mapper.DatasetMetadataMapper;
import com.bupt.ilink.service.*;
import com.bupt.ilink.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
public class DatasetInformationController {
    @Autowired
    private DatasetMetadataMapper datasetMetadataMapper;

    @Autowired
    private DatasetTagsService datasetTagsService;

    @Autowired
    private DatasetIntroductionService databaseIntroductionService;

    @Autowired
    private DatasetPublishService datasetPublishService;

    @Autowired
    private OssService ossService;

    //  【详情页】-【元数据信息】
    @GetMapping("/getDatasetMetadataById")
    public R getDatasetMetadataById(@RequestParam String targetId){
        DatasetMetadata metadata = datasetMetadataMapper.selectById(targetId);
        if(metadata != null){
            DatasetResponse response = new DatasetResponse();
            response.setDatasetId(Long.valueOf(targetId));
            response.setName(metadata.getName());

            List<String> tags = datasetTagsService.list(
                    new QueryWrapper<DatasetTags>()
                            .eq("dataset_id", targetId)
            ).stream().map(DatasetTags::getTag).collect(Collectors.toList());;
            response.setTags(tags);

            response.setCreatorName(metadata.getCreatorName());
            response.setDownloadNum(metadata.getDownloadNum());
            response.setClickNum(metadata.getClickNum());
            response.setFavoriteNum(metadata.getFavoriteNum());
            response.setShareNum(metadata.getShareNum());
            response.setCreatedTime(metadata.getCreatedTime());

            return R.ok(response);
        }
        return R.failure("targetId元数据信息 未找到");
    }

    //  【详情页】-【数据集介绍】
    @GetMapping("/getDatasetsIntroduction")
    public R getDatasetsIntroduction(@RequestParam String targetId){
        DatasetIntroduction introduction = databaseIntroductionService.selectById(targetId);
        if(introduction != null){
            return R.ok(introduction);
        }
        return R.failure("targetId数据集介绍 未找到");
    }

    //  【详情页】-【发布信息】
    @GetMapping("/getDatasetsPublish")
    public R getDatasetsPublish(@RequestParam String targetId){
        DatasetPublish publish = datasetPublishService.selectById(targetId);
        if(publish != null){
            return R.ok(publish);
        }
        return R.failure("targetId数据集发布信息 未找到");
    }

    //  【详情页】-【Readme文件】
    @GetMapping("/getDatasetsReadme")
    public R getDatasetsReadme(@RequestParam String targetId){
        String datasetUrl = ossService.getBucketFileUrl("dataset-square", "readme/"+targetId+".pdf");
        if(datasetUrl != null && !datasetUrl.isEmpty()){
            return R.ok(datasetUrl);
        }
        return R.failure("targetId文件详情 未找到");
    }
}